package product;

import java.awt.Color;
import java.awt.Event;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;


public class ProductMain extends JFrame {
	ArrayList<ProductData> pro = new ArrayList<ProductData>();
	HashMap<String, CodeData> cod = new HashMap<String, CodeData>();
	static int count=0; // 생산중인 제품의 수
	int saleCnt=0;//판매 번호
	
	ArrayList<SaleData> aSal = new ArrayList<SaleData>();
	HashMap<String, SaleTotal> hSal = new HashMap<String, SaleTotal>();
	HashMap<String, StockData> sto = new HashMap<String, StockData>();
	
	CodeInput codeInput = null;
	CodeUpdate codeUpdate = null;
	ProductInput productInput = null;
	ProductUpdate productUpdate = null;
	Sale sale = null;
	SaleList saleList= null;
	Stock stock =null;

	JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;
	private JMenu mnNewMenu_1;
	private JMenuItem mntmNewMenuItem_4;
	private JMenuItem mntmNewMenuItem_5;
	private JMenuItem mntmNewMenuItem_6;

	
	JMenuBar menubar = new JMenuBar();
	JMenu file = new JMenu("관리(F)");
	//file.setMnemonic(KeyEvent.VK_F); // alt + F
	//menubar.add(file);
	  
	JMenuItem fileLoad = new JMenuItem("파일로드");
	//fileLoad.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK)); // ctrl + L
	//file.add(fileLoad);
	
	
	
	

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductMain frame = new ProductMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProductMain() {
		setTitle("PRODUCT");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1050, 600);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		load();
	}
	public static void setCount(int cnt) {
		count = cnt;
	}
	
	public void load() {
		try {
			FileInputStream fr = new FileInputStream("CodeList.dat");
			ObjectInputStream br = new ObjectInputStream(fr);
			cod = (HashMap)br.readObject();
			br.close();
			fr.close();			
		} catch(Exception e) {
			System.out.println("CodeList.dat 로드 실패");
		}
		try {			
			FileInputStream fr = new FileInputStream("ProductList.dat");
			ObjectInputStream br = new ObjectInputStream(fr);
			pro = (ArrayList) br.readObject();
			br.close();
			fr.close();			
		} catch(Exception e) {
			System.out.println("ProductList.dat 로드 실패");
		}
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("count.ser"));
			count = (Integer)ois.readObject();
			ois.close();
		} catch(Exception e) {
			System.out.println("count.ser 로드 실패");
		} 
		try {			
			FileInputStream fr = new FileInputStream("StockList.dat");
			ObjectInputStream br = new ObjectInputStream(fr);
			sto = (HashMap) br.readObject();
			br.close();
			fr.close();			
		} catch(Exception e) {
			System.out.println("StockList.dat 로드 실패");
		}
		try {			
			FileInputStream fr = new FileInputStream("SaleList.dat");
			ObjectInputStream br = new ObjectInputStream(fr);
			aSal = (ArrayList) br.readObject();
			br.close();
			fr.close();			
		} catch(Exception e) {
			System.out.println("SaleList.dat 로드 실패");
		}
		try {			
			FileInputStream fr = new FileInputStream("SaleTotal.dat");
			ObjectInputStream br = new ObjectInputStream(fr);
			hSal = (HashMap) br.readObject();
			br.close();
			fr.close();			
		} catch(Exception e) {
			System.out.println("SaleTotal.dat 로드 실패");
		}
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("saleCnt.ser"));
			saleCnt = (Integer)ois.readObject();
			ois.close();
		} catch(Exception e) {
			System.out.println("saleCnt.ser 로드 실패");
		}
	}

	private JMenuBar getMenuBar_1() {
		if(menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
			menuBar.add(getMnNewMenu_1());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if(mnNewMenu == null) {
			mnNewMenu = new JMenu("관리");
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_3());
			mnNewMenu.add(getMntmNewMenuItem_6());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if(mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("CodeInput");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					JMenuItem fileLoad = new JMenuItem("CodeInput");
					fileLoad.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK));
					file.add(fileLoad);
					
					
					
					
					
					if(codeInput == null) {
						codeInput = new CodeInput(cod);
						codeInput.setBounds(20, 20, 230, 180);
						contentPane.add(codeInput);
						codeInput.setVisible(true);
					}else if(codeInput != null){
						codeInput.setVisible(false);
						codeInput=null;
					}
					
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if(mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("CodeUpdate");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(codeUpdate == null) {
						codeUpdate = new CodeUpdate(cod);
						codeUpdate.setBounds(20, 230, 230, 300);
					contentPane.add(codeUpdate);
					codeUpdate.setVisible(true);
					}else if(codeUpdate != null){
						codeUpdate.setVisible(false);
						codeUpdate=null;
					}

				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if(mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("ProductInput");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(productInput == null) {
						productInput = new ProductInput(cod,pro,count);
						productInput.setBounds(280, 20, 260, 280);
					contentPane.add(productInput);
					productInput.setVisible(true);
					}else if(productInput != null){
						productInput.setVisible(false);
						productInput=null;
					}
				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if(mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("ProductUpdate");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (productUpdate == null) {
						productUpdate = new ProductUpdate(pro);
						productUpdate.setBounds(550, 20, 488, 312);
						contentPane.add(productUpdate);
						productUpdate.setVisible(true);
					} else if(productUpdate != null){
						productUpdate.setVisible(false);
						productUpdate=null;
					}
				}
			});
		}
		return mntmNewMenuItem_3;
	}
	private JMenu getMnNewMenu_1() {
		if (mnNewMenu_1 == null) {
			mnNewMenu_1 = new JMenu("판매");
			mnNewMenu_1.add(getMntmNewMenuItem_4());
			mnNewMenu_1.add(getMntmNewMenuItem_5());
		}
		return mnNewMenu_1;
	}
	private JMenuItem getMntmNewMenuItem_4() {
		if (mntmNewMenuItem_4 == null) {
			mntmNewMenuItem_4 = new JMenuItem("Sale");
			mntmNewMenuItem_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (sale == null) {
						sale = new Sale(sto,aSal,hSal,saleCnt);
						sale.setBounds(20, 20, 300 , 312);
						contentPane.add(sale);
						sale.setVisible(true);
					}else if(sale != null){
						sale.setVisible(false);
						sale=null;
						try {
							ObjectInputStream ois = new ObjectInputStream(new FileInputStream("saleCnt.ser"));
							saleCnt = (Integer)ois.readObject();
							ois.close();
						} catch(Exception e) {
							System.out.println("saleCnt.ser 로드 실패");
						}
					}
				}
			});
		}
		return mntmNewMenuItem_4;
	}
	private JMenuItem getMntmNewMenuItem_5() {
		if (mntmNewMenuItem_5 == null) {
			mntmNewMenuItem_5 = new JMenuItem("SaleList");
			mntmNewMenuItem_5.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (saleList == null) {
						saleList = new SaleList(aSal,hSal,sto,ProductMain.this);
						saleList.setBounds(330, 20, 700, 312);
						contentPane.add(saleList);
						saleList.setVisible(true);
					}else if(saleList != null){
						saleList.setVisible(false);
						saleList=null;
					}
				}
			});
		}
		return mntmNewMenuItem_5;
	}
	private JMenuItem getMntmNewMenuItem_6() {
		if (mntmNewMenuItem_6 == null) {
			mntmNewMenuItem_6 = new JMenuItem("재고추가");
			mntmNewMenuItem_6.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (stock == null) {
						stock = new Stock(sto);
						stock.setBounds(550, 20, 488, 312);
						contentPane.add(stock);
						stock.setVisible(true);
					}else if(stock != null){
						stock.setVisible(false);
						stock=null;
					}
				
					
				}
			});
		}
		return mntmNewMenuItem_6;
	}
}